package com.gzhryc.shared_device.oem.h5.mng.controllers.market.account;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.net_marketing.cloud.account.dao.db.*;
import com.gzhryc.net_marketing.cloud.account.services.*;
import com.gzhryc.net_marketing.cloud.account.services.dto.*;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import com.gzhryc.net_marketing.cloud.account.dao.enums.CloudPayAccountEnum;
import com.gzhryc.system.SysConstants;
import com.gzhryc.system.dao.db.Manager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class CloudPayAccountController {

    @Route(value = { "/market/cloud/account/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("stateList", CloudPayAccountEnum.State.values());
        return "/market/cloud/account/list.html";
    }

    @Route(value = "/market/cloud/account/list.data")
    public ListResult<CloudPayAccount> listData(HttpServletRequest request) {
        ListResult<CloudPayAccount> result = new ListResult<CloudPayAccount>(DtoConstants.NULL_DATA, "暂无数据");
        CloudPayAccountSearch search = WebUtils.getParameter(request, CloudPayAccountSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(CloudPayAccountService.self().countJoinBySearch(search));
        result.getBody().setData(CloudPayAccountService.self().findJoinBySearch(search, pageNo, pageLimit));

        if (result.getBody().getData() != null) {
            result.setCode(DtoConstants.SUCCESS,null);
        }
        return result;
    }

    @Route(value = { "/market/cloud/account/info.html" })
    public String info(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        if(id != null){
            CloudPayAccount cloudPayAccount = CloudPayAccountService.self().getJoinById(id);
            WebUtils.setAttribute(request,cloudPayAccount);
        }
        return "/market/cloud/account/info.html";
    }

    @Route(value = { "/market/cloud/account/update.html" })
    public String update(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        if(id != null){
            CloudPayAccount cloudPayAccount = CloudPayAccountService.self().getById(id);
            WebUtils.setAttribute(request,cloudPayAccount);
        }
        return "/market/cloud/account/edit.html";
    }

    @Route(value = { "/market/cloud/account/update.do" })
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"修改失败，请稍后再试");
        CloudPayAccount cloudPayAccount = WebUtils.getParameter(request,CloudPayAccount.class);
        try {
            if(CloudPayAccountService.self().updateNotNull(cloudPayAccount)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/market/cloud/account/recharge.html" })
    public String recharge(HttpServletRequest request) {
        return "/market/cloud/account/recharge.html";
    }

    @Route(value = { "/market/cloud/account/recharge.do" })
    public AjaxResult doRecharge(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"修改失败，请稍后再试");
        Long id = WebUtils.getLong(request,"id");
        Double money = WebUtils.getDouble(request,"money");
        String reason = WebUtils.getString(request,"reason");
        if(id != null && money != null && StringTools.isNotBlank(reason)) {
            Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
            if (manager == null) {
                result.setCode(DtoConstants.LOGIN_OVERDUE, "登录过期，请重新登录");
                return result;
            }
            try {
                if (CloudPayAccountService.self().artificialRecharge(id,money,reason,manager)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = "/market/cloud/account/operate/list.data")
    public ListResult<CloudPayAccountOperateRecord> operateRecordListData(HttpServletRequest request) {
        ListResult<CloudPayAccountOperateRecord> result = new ListResult<CloudPayAccountOperateRecord>(DtoConstants.NULL_DATA, "暂无数据");
        CloudPayAccountOperateRecordSearch search = WebUtils.getParameter(request, CloudPayAccountOperateRecordSearch.class);
        if(search.getAccountId() != null) {
            Integer pageNo = WebUtils.getInteger(request, "page", 1);
            Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

            result.getBody().setTotal(CloudPayAccountOperateRecordService.self().countBySearch(search));
            result.getBody().setData(CloudPayAccountOperateRecordService.self().findBySearch(search, pageNo, pageLimit));

            if (result.getBody().getData() != null) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        }
        return result;
    }

    /**
     * 获取充值订单
     * @param request
     * @return
     */
    @Route(value = "/market/cloud/account/order/deposit/list.data")
    public ListResult<CloudPayAccountDepositOrder> depositOrderListData(HttpServletRequest request) {
        ListResult<CloudPayAccountDepositOrder> result = new ListResult<CloudPayAccountDepositOrder>(DtoConstants.NULL_DATA, "暂无数据");
        CloudPayAccountDepositOrderSearch search = WebUtils.getParameter(request, CloudPayAccountDepositOrderSearch.class);
        if(search.getAccountId() != null) {
            Integer pageNo = WebUtils.getInteger(request, "page", 1);
            Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

            result.getBody().setTotal(CloudPayAccountDepositOrderService.self().countBySearch(search));
            result.getBody().setData(CloudPayAccountDepositOrderService.self().findBySearch(search, pageNo, pageLimit));

            if (result.getBody().getData() != null) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        }
        return result;
    }

    /**
     * 获取交易订单
     * @param request
     * @return
     */
    @Route(value = "/market/cloud/account/order/list.data")
    public ListResult<CloudPayAccountExpenditureOrder> orderListData(HttpServletRequest request) {
        ListResult<CloudPayAccountExpenditureOrder> result = new ListResult<CloudPayAccountExpenditureOrder>(DtoConstants.NULL_DATA, "暂无数据");
        Integer type = WebUtils.getInteger(request,"type");
        Long accountId = WebUtils.getLong(request,"accountId");
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        if(type != null && accountId != null){
            if(type == 1){
                //收入
                CloudPayAccountExpenditureOrderSearch search = new CloudPayAccountExpenditureOrderSearch();
                search.setPayeeAccountId(accountId);

                result.getBody().setTotal(CloudPayAccountExpenditureOrderService.self().countBySearch(search));
                result.getBody().setData(CloudPayAccountExpenditureOrderService.self().findBySearch(search, pageNo, pageLimit));

                if (result.getBody().getData() != null) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            }else if(type == 2){
                //支出
                CloudPayAccountExpenditureOrderSearch search = new CloudPayAccountExpenditureOrderSearch();
                search.setAccountId(accountId);

                result.getBody().setTotal(CloudPayAccountExpenditureOrderService.self().countBySearch(search));
                result.getBody().setData(CloudPayAccountExpenditureOrderService.self().findBySearch(search, pageNo, pageLimit));

                if (result.getBody().getData() != null) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            }
        }
        return result;
    }

    /**
     * 获取取款订单
     * @param request
     * @return
     */
    @Route(value = "/market/cloud/account/order/withdraw/list.data")
    public ListResult<CloudPayAccountWithdrawOrder> withdrawOrderListData(HttpServletRequest request) {
        ListResult<CloudPayAccountWithdrawOrder> result = new ListResult<CloudPayAccountWithdrawOrder>(DtoConstants.NULL_DATA, "暂无数据");
        CloudPayAccountWithdrawOrderSearch search = WebUtils.getParameter(request, CloudPayAccountWithdrawOrderSearch.class);
        if(search.getAccountId() != null) {
            Integer pageNo = WebUtils.getInteger(request, "page", 1);
            Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

            result.getBody().setTotal(CloudPayAccountWithdrawOrderService.self().countBySearch(search));
            result.getBody().setData(CloudPayAccountWithdrawOrderService.self().findBySearch(search, pageNo, pageLimit));

            if (result.getBody().getData() != null) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        }
        return result;
    }

    @Route(value = { "/market/cloud/account/withdraw/payee/list.data" })
    public ListResult<CloudPayAccountWithdrawPayee> withdrawPayeeListData(HttpServletRequest request) {
        ListResult<CloudPayAccountWithdrawPayee> result = new ListResult<CloudPayAccountWithdrawPayee>(DtoConstants.NULL_DATA, "暂无数据");
        CloudPayAccountWithdrawPayeeSearch search = WebUtils.getParameter(request, CloudPayAccountWithdrawPayeeSearch.class);
        if(search.getAccountId() != null) {
            Integer pageNo = WebUtils.getInteger(request, "page", 1);
            Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

            result.getBody().setTotal(CloudPayAccountWithdrawPayeeService.self().countJoinBySearch(search));
            result.getBody().setData(CloudPayAccountWithdrawPayeeService.self().findJoinBySearch(search, pageNo, pageLimit));

            if (result.getBody().getData() != null) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        }
        return result;
    }

    @Route(value = { "/market/cloud/account/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败，请稍后再试");
        Long id = WebUtils.getLong(request,"id");
        try {
            if(CloudPayAccountService.self().deleteById(id)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
